﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Memcached.ClientLibrary;

/// <summary>
///MemcachedTools 的摘要说明
/// </summary>
public class MemcachedTools
{

    static MemcachedTools()
    {
        String[] serverlist = System.Configuration.ConfigurationManager.AppSettings["newsMemcache"].Split(',') ;
        //String[] serverlist = { "172.28.1.200:11211", "172.28.1.201:11211" };
        SockIOPool pool = SockIOPool.GetInstance("news");
        pool.SetServers(serverlist);
        pool.Initialize();

        String[] serverlist1 =  System.Configuration.ConfigurationManager.AppSettings["hqMemecache"].Split(',') ;
        //String[] serverlist1 = { "172.28.1.71:11211" };
        SockIOPool pool2 = SockIOPool.GetInstance("hangqing");
        pool2.SocketTimeout = 5000;
        pool2.SocketConnectTimeout = 5000;
        pool2.SetServers(serverlist1);
        pool2.Initialize();
    }

    private static MemcachedClient NewsClient
    {
        get
        {
            MemcachedClient mc = new MemcachedClient();
            mc.PoolName = "news";
            mc.EnableCompression = false;
            return mc;
        }
    }

    public static void Clear(string key)
    {
        NewsClient.Delete(key);
    }

    public static void ClearAll()
    {
        NewsClient.FlushAll();
    }

    public static void ClearLike(string key)
    {
        CMSModel.CMSEntities dal = new CMSModel.CMSEntities();
        string[] keys = dal.MemcacheKey.Where(x => x.Key.Contains(key)).Select(x => x.Key).ToArray();
        foreach (var c in keys)
        {
            Clear(c);
        }
    }
}